<template>
  <hi-row
    isColumn
    :wrap="false"
    class="viewcontainer"
    v-if="dataset && subDataSet"
  >
    <hi-col flex="340px">
      <hi-toolbar
        :calloutcofig="calloutcofig"
        :source="dataset"
        v-bind="toolbar"
        ref="dbtoolbar"
        :addOne="true"
        :disabled="dataset.isReadonly()"
      ></hi-toolbar>
      <btn-toolbar @callFunc="clickBtn" class="btn-toolbar"></btn-toolbar>
      <print
        :form-data="dataset.data[0]"
        :table-data="subDataSet.data"
        ref="print"
        v-if="dataset.data[0] && subDataSet.data"
      ></print>
      <hi-form
        :dataSource="dataset"
        ref="detailForm"
        :model="dataset.curRecord"
        v-bind="form"
        :disabled="dataset.isReadonly()"
      ></hi-form>
    </hi-col>
    <hi-col flex="auto" :min-height="200">
      <hi-table
        ref="detailTable"
        :dataSource="subDataSet"
        v-bind="dataTable"
        :model="subDataSet.data"
        :isEdit="!subDataSet.isReadonly()"
      ></hi-table>
    </hi-col>
  </hi-row>
</template>

<script>
import Print from "./print";
import BtnToolbar from "@/views/component/toolbar/index.vue";
import {
  HiForm,
  DataSetContainer,
  HiToolbar,
  HiTable,
  HiNumber,
  utils,
} from "hi-ui";
import viewBase from "../../base";
import { cloneDeep } from "lodash";
import toolbar from "@/hiuiDefaultConfig/toolbar";
import purchaseSettlementConfig from "./dataset/settlementlist.js";
import purchaseSettlementDetailConfig from "./dataset/settlementdetail.js";
import inspectlist from "./calculate.js";
import moneyAccountCfg from "../../database/moneyAccount/dataset";
import audittag from "@/views/component/audittag";
export default {
  name: "settlementdetail",
  extends: viewBase,
  components: {
    HiForm,
    HiToolbar,
    HiTable,
    HiNumber,
    audittag,
    BtnToolbar,
    Print,
  },

  data() {
    return {
      massage: null,
      calloutcofig: {
        /*     param: {
          FAUDIT: 0,
        },  */
        storeCfg: inspectlist,
        active: true,
        isAppend: true,
        /* treeOption:{
                            "modelFile":"users",
                            label: "name",
                            children: "company"
                        }, */

        multiSelect: true,
        allOrderReturn: false,
        columns: [
          {
            name: "FNUMBER",
            label: "单号",
            width: 140,
          },
          {
            name: "FSUPPLIER",
            label: "供应商",
            width: 140,
          },
          {
            name: "FCOUNT",
            label: "类目数",
            formType: "dbDouble",
          },
          {
            name: "FPAYAMOUNT",
            label: "件数",
            formType: "dbDouble",
            props: {
              precision: 2,
              // format: "0.00",
              "controls-position": "right",
            },
          },

          {
            name: "FBILLMONEY",
            label: "单据金额",
            formType: "dbDouble",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FPAYMONEY",
            label: "未付金额",
            formType: "dbDouble",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
        ],
        queryOption: {
          fields: [
            {
              name: "FNUMBER",
              element: "el-input",
              label: "单号",
            },
            {
              element: "el-input",
              name: "FSUPPLIER",
              label: "供应商",
            },
          ],
        },
        details: [
          /*   {
            target: {
              id: "SettlementEntity",
            }, //目标
            source: {
              id: "InspectDetailEntity",
              modelFile: "purchase/inspect/dataset/InspectDetailEntity.xml",
            }, //来源
            // queryCdions: {}, //来源数据集查询条件
            updateFields: [], //更新字段
            limitEqualsFields: [], //相同字段
            uniqueFields: [], //主键
            returnFields: {
              FSUPPLIER: "FSUPPLIER",
              FNUMBER1: "FNUMBER",
            }, //返回字段
          },*/
          {
            target: {
              id: "SettlementDetailEntity",
            }, //目标
            source: {
              id: "CalculateEntity",
              modelFile: "purchase/settlement/dataset/CalculateEntity.xml",
            }, //来源 //来源数据集查询条件
            /*   queryCdions: {
              FNUMBER: function (rec) {
                return rec["FNUMBER1"];
              },
            },  */ updateFields: [], //更新字段
            limitEqualsFields: ["FSUPPLIER"], //相同字段
            uniqueFields: ["FISNUMBER"], //唯一字段
            returnFields: {
              //返回字段
              FISNUMBER: "FNUMBER",
              FORDERMONEY: "FBILLMONEY",
              FAMOUNTPAYABLE: "FPAYMONEY",
              FCOUNT: "FCOUNT",
              FAMOUNT: "FPAYAMOUNT",
              FSUPPLIER: "FSUPPLIER",
              FSUPPLIERID: "FSUPPLIERID",
            },
          },
        ],
      },

      //数据集
      dataset: null,
      //数据集配置
      datasetCfg: purchaseSettlementConfig,
      //从表数据集
      subDataSet: null,
      //从表数据集配置
      subDatasetCfg: purchaseSettlementDetailConfig,

      //数据桥梁
      dataBridge: new Map(),
      //数据表格
      dataTable: {
        hasOperAdd: false,
        isEdit: true,
        hasSelection: true,
        hasRowNumber: true,
        columns: [
          {
            name: "FISNUMBER",
          },
          {
            name: "FCOUNT",
          },
          {
            name: "FAMOUNT",
            props: {
              min: 0,
              precision: 2,
            },
          },
          {
            name: "FORDERMONEY",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FAMOUNTPAYABLE",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FPAYABLE",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
        ],
      },
      toolbar: [],
      //表单配置
      form: {
        columnsTotal: 2,
        items: [
          {
            name: "FNUMBER",
          },
          {
            name: "FSUPPLIER",
            /*  element: "hi-data-select",
            props: {
              lookup: {
                storeCfg: orderCfg,
                active: false,

                multiSelect: false,
                returnFields: {
                  FSUPPLIER: "FSUPPLIER",

                },
                columns: [
                  {
                    name: "FSUPPLIER",
                    label: "供应商",

                  },

                ],
                queryOption: {
                  fields: [
                    {
                      name: "FSUPPLIER",
                      label: "供应商",
                      element: "el-input",
                    },

                  ],
                },
              },
            },*/
          },
          {
            name: "FPAYWAY",
            element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",
                param: { FPID: "fkfs" },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FPAYWAY: "FDESC",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "付款方式",
                  },
                ],
              },
            },
          },
          {
            name: "FPAYMONEY",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
          /*    {
            name: "FMONEYACCOUNT",
            element: "hi-data-select",
            props: {
              lookup: {
                storeCfg: moneyAccountCfg,
                active: true,

                checkValid: true,
                multiSelect: false,
                returnFields: {
                  FMONEYACCOUNT: "FDESC",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "通用名称",
                  },
                  {
                    name: "FUSERID",
                    label: "用户",
                  },
                  {
                    name: "FCREATEDATE",
                    label: "时间",
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "FDESC",
                      label: "通用名称",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          }, */

          {
            name: "FNOTE",
            colspan: 2,
            props: {
              type: "textarea",
              autosize: { minRows: 4, maxRows: 4 },
              maxlength: 200,
            },
          },

          {
            name: "FUSERID",
          },
          {
            name: "FCREATEDATE",
          },
          {
            element: audittag,
            name: "FAUDIT",
          },
        ],
      },
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    clickBtn(methodName) {
      switch (methodName) {
        case "print":
          this.$refs.print.onPrint();
          break;
      }
    },
    addDetail() {
      this.subDataSet.add({});
    },
    //更新供应商
    updateSupplier(data) {
      if (data.FSUPPLIER) {
        this.dataset.data[0].FSUPPLIER = data.FSUPPLIER;
        this.dataset.data[0].FSUPPLIERID = data.FSUPPLIERID;
      }
    },
    //更新付款金额
    updateFPAYABLE(data, store, options) {
      if (options.field == "FPAYABLE") {
        let allData = store.data,
          sum = 0;
        for (let i = 0; i < allData.length; i++) {
          let item = allData[i];
          sum = utils.number.accAdd(sum, item.FPAYABLE || 0);
        }
        this.dataset.data[0].FPAYMONEY = sum;
      }
    },
  },
  created() {
    this.toolbar = cloneDeep(toolbar);
    this.toolbar.buttons.push({
      kind: "callout",
    });
  },
  mounted() {
    //创建主数据集
    this.dataset = this.createDataSet(this.datasetCfg);

    //创建从数据集
    this.subDataSet = this.createDataSet(this.subDatasetCfg, this.dataset);
    let me = this;
    this.subDataSet.on("afterchange", (model, store, options) => {
      me.updateSupplier(model.data);
      me.updateFPAYABLE(model.data, store, options);
    });
    this.subDataSet.on("afteradd", (data, store) => {
      me.updateSupplier(data[0]);
    });
    //设置数据与组件的关系
    this.dataBridge.set(this.dataset, ["detailForm", "detailTable"]);
    this.dataBridge.set(this.subDataSet, []);
    this.flowConfig = { dataset: this.dataset };

    if (this.$route.query.id) {
      this.dataset.load({
        FID: this.$route.query.id,
      });
    }
    console.log("表单---------", this.subDataSet);
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scope></style>
